<template>
  <div class="app-container">
    <el-form :inline="true" :model="formData " class="demo-form-inline">
      <el-form-item label="回库申请单编号">
        <el-input v-model="formData.returnApplyNo" placeholder="请输入回货入库编号"></el-input>
      </el-form-item>
      <el-form-item label="客户名称">
        <el-input v-model="formData.customerName" placeholder="请输入客户名称"></el-input>
      </el-form-item>
      <el-form-item label="租赁销售订单编号">
        <el-input v-model="formData.orderNo" placeholder="请输入回货申请单标号"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
        <el-button @click="resetList">重置</el-button>
      </el-form-item>
    </el-form>
    <el-card>
      <el-button v-hasPermi="['module:apply:add']" style="margin-bottom: 10px" type="success" icon="el-icon-plus" @click="openFn(0)">新增</el-button>

      <el-table
        :data="list"
        border
        style="width: 100%">
        <el-table-column
          label="产品信息"
          type="expand"
          width="55">
          <!--          回库产品-->
          <template slot-scope="scope">
            <div style="margin-left:250px; ">
              <el-table
                :data="scope.row.childReturnApply"
                stripe
                style="width: 70%;">

                <el-table-column
                  prop="returnApplyNo"
                  label="回库申请单编号"
                  align="center"
                  width="180">
                </el-table-column>
                <el-table-column
                  prop="orderNo"
                  width="180"
                  align="center"
                  label="订单编号">
                </el-table-column>

                <el-table-column
                  prop="createTime"
                  width="150"
                  align="center"
                  label="创建时间">
                </el-table-column>
                <el-table-column
                  prop="customerName"
                  width="150"
                  align="center"
                  label="客户名称">
                </el-table-column>
                <el-table-column
                  prop="orderStatus"
                  width="150"
                  align="center"
                  label="订单状态">
                  <template slot-scope="scope">
                    <el-button type="text" v-if="scope.row.orderStatus==1">已提交</el-button>
                    <el-button type="text" v-if="scope.row.orderStatus==2">已入库</el-button>
<!--                    <el-button type="text" v-if="scope.row.orderStatus==3">已完成</el-button>-->
<!--                    <el-button type="text" v-if="scope.row.orderStatus==4">终结</el-button>-->
                  </template>
                </el-table-column>
                <el-table-column label="产品详情" prop="productList" width="120">
                  <template slot-scope="scope">
                    <el-button type="text" @click="productInfo(scope.row)">查看详情</el-button>
                  </template>
                </el-table-column>
                <el-table-column label="操作"   width="120">
                  <template slot-scope="scope">
                    <el-button type="text" @click="logisticsBtn(scope.row)">修改物流</el-button>
                  </template>
                </el-table-column>

              </el-table>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          fixed
          prop="returnType"
          label="回货/退货类型"
          width="150">
          <template slot-scope="scope">
            <el-button type="text" v-if="scope.row.returnType==1">回货</el-button>
            <el-button type="text" v-if="scope.row.returnType==2">退货</el-button>
          </template>

        </el-table-column>
        <el-table-column
          prop="sponsorName"
          label="发起人"
          width="120">
        </el-table-column>

        <el-table-column
          prop="customerName"
          label="客户名称"
          width="120">
        </el-table-column>
        <el-table-column
          prop="orderNo"
          label="订单编号"
          width="150">
        </el-table-column>
        <el-table-column
          prop="loadingAddressProvince"
          label="装货省份"
          width="120">
        </el-table-column>
        <el-table-column
          prop="loadingAddressCity"
          label="装货地址市"
          width="120">
        </el-table-column>
        <el-table-column
          prop="loadingAddressArea"
          label="装货地址区"
          width="120">
        </el-table-column>
        <el-table-column
          prop="loadingAddress"
          label="详细地址"
          width="120">
        </el-table-column>
        <el-table-column
          prop="contactName"
          label="对接人名称"
          width="120">
        </el-table-column>
        <el-table-column
          prop="contactTelephone"
          label="联系电话"
          width="120">
        </el-table-column>
        <el-table-column
          prop="whetherLogisticsRequired"
          label="是否需要物流安排车辆"
          width="120">
           <template slot-scope="scope">
             <el-button v-if="scope.row.whetherLogisticsRequired==='1'">是</el-button>
             <el-button v-if="scope.row.whetherLogisticsRequired==='2'">是</el-button>
           </template>
        </el-table-column>
        <el-table-column
          prop="collaboratorName"
          label="协同人"
          width="120">

        </el-table-column>
        <el-table-column
          prop="totalDailyRent"
          label="日租金合计"
          width="120">

        </el-table-column>
        <el-table-column
          prop="orderStatus"
          label="订单状态"
          width="120">
           <template slot-scope="scope">
             <el-button type="text" v-if="scope.row.orderStatus==1">已提交</el-button>
             <el-button type="text" v-if="scope.row.orderStatus==2">已入库</el-button>
<!--             <el-button type="text" v-if="scope.row.orderStatus==3">已完成</el-button>-->
<!--             <el-button type="text" v-if="scope.row.orderStatus==4">终结</el-button>-->
           </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="100">
          <template slot-scope="scope">
            <el-button  v-hasPermi="['module:apply:edit']" type="text" size="small" @click="editFn(scope.row)">编辑</el-button>
            <el-button  v-hasPermi="['module:apply:remove']" type="text" size="small" @click="delFn(scope.row.id)">删除</el-button>
            <el-button  v-hasPermi="['module:apply:add']" type="text" size="small" @click="openFn(scope.row)">添加</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="formData.pageNum"
        :page-sizes="[10, 15, 20, 25]"
        :page-size="formData.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
    <!--  新增弹框-->
    <el-dialog
      :title="this.formData.id?'修改回库申请单':'添加回库申请单'"
      :visible.sync="isShow"
      width="40%"
      @close="closeDialog"
    >
      <el-form :model="formData" :rules="formRules" ref="form" inline label-width="100px">
        <el-form-item label="租赁销售订单" prop="orderNo">
          <el-input style="width:200px" v-model="formData.orderNo">
            <template slot="append">
              <el-button style="width:60px" type="primary" @click="openOrderListFn">
                选择
              </el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="回货单编号" prop="returnApplyNo">
          <el-input v-model="formData.returnApplyNo"></el-input>
        </el-form-item>
        <el-form-item label="回货/退货类型" prop="returnType">
          <el-select v-model="formData.returnType" placeholder="请选择">
            <el-option
              v-for="item in Ordertypes"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发起人名称" prop="sponsorName">
          <el-input v-model="formData.sponsorName"></el-input>
        </el-form-item>
        <el-form-item label="协同人名称" prop="collaboratorName">
          <el-input v-model="formData.collaboratorName"></el-input>
        </el-form-item>
        <el-form-item label="客户名称" prop="customerName">
          <el-input v-model="formData.customerName"></el-input>
        </el-form-item>
        <el-form-item label="对接人名称" prop="contactName">
          <el-input v-model="formData.contactName"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="contactTelephone">
          <el-input v-model="formData.contactTelephone"></el-input>
        </el-form-item>
        <!--  <el-form-item label="装货地址省份" prop="loadingAddressProvince"><el-input v-model="formData.loadingAddressProvince"></el-input></el-form-item>-->
        <!--  <el-form-item label="装货地址市" prop="loadingAddressCity"><el-input v-model="formData.loadingAddressCity"></el-input></el-form-item>-->
        <!--  <el-form-item label="装货地址区" prop="loadingAddressArea"><el-input v-model="formData.loadingAddressArea"></el-input></el-form-item>-->
          <el-form-item label="选择地址">
            <el-cascader
              style="width: 300px"
              v-model="formData.loadingAddressOptions"
              :options="loadingAddressOptions"
              :props="optionProps"
              placeholder="请选择收获地址"
              @change="changeFn"
              filterable
               ></el-cascader>
          </el-form-item>
          <el-form-item label="装货详细地址" prop="loadingAddress"><el-input style="width: 500px" v-model="formData.loadingAddress">

          </el-input></el-form-item>


        <el-form-item label="装货日期" prop="loadingDate">
          <el-date-picker
            v-model="formData.loadingDate"
            type="date"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label-width="180px" label="是否需要物流安排车辆" prop="whetherLogisticsRequired">
          <el-select v-model="formData.whetherLogisticsRequired" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="日租金合计" prop="totalDailyRent">
          <el-input v-model="formData.totalDailyRent"></el-input>
        </el-form-item>
<!--        <el-form-item label="运输方式" prop="shippingType">-->
<!--          <el-input v-model="formData.shippingType"></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="司机姓名" prop="driverName">-->
<!--          <el-input v-model="formData.driverName"></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="司机联系方式" prop="driverTel">-->
<!--          <el-input v-model="formData.driverTel"></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="车辆信息" prop="vehicleInformation">-->
<!--          <el-input v-model="formData.vehicleInformation"></el-input>-->
<!--        </el-form-item>-->
        <el-form-item label="仓库名称" prop="warehouseName" >
          <el-input v-model="formData.warehouseName" style="width: 200px">
            <template slot="append">
              <el-button style="width:60px" type="primary" @click="openImportWarehouse">选择</el-button>
            </template>
          </el-input>

        </el-form-item>
        <br/>
        <el-form-item v-if="formData.parentId" label="产品信息" prop="productList">
          <div style="display:flex;">
            <label class="whiteBankCard"
                   style="width: 130px;margin-right: 10px;text-align: center">产品名称</label>
            <label class="whiteBankCard"
                   style="width: 130px;margin-right: 10px;text-align: center">型号</label>
            <label class="whiteBankCard"
                   style="width: 130px;margin-right: 10px;text-align: center">数量</label>
            <!--      <label class="whiteBankCard"-->
            <!--             style="width: 130px;margin-right: 10px;text-align: center">仓库</label>-->


            <label class="whiteBankCard"
                   style="width: 100px;margin-right: 10px;text-align: center">操作</label>

          </div>
          <div style="display:flex;" v-for="(item,index) in  formData.productList" :key="index">
            <div :prop="'product.' + index">
              <el-input class="whiteBankCard" readonly v-model="item.productName" placeholder="产品名称"
                        style="width: 130px;margin-right: 10px"></el-input>
              <el-input class="whiteBankCard" readonly v-model="item.model" placeholder="型号"
                        style="width: 130px;margin-right: 10px"/>
              <el-input class="whiteBankCard" v-model="item.returnQuantity" @blur="isQuantityFn(item,index)" placeholder="数量"
                        style="width: 130px;margin-right: 10px"/>
              <!--        <el-input class="whiteBankCard"    placeholder="仓库"-->
              <!--                  style="width: 130px;margin-right: 10px"/>-->

              <el-button type="danger" size="mini" style="height: 30px; margin-left:20px" @click="removeProduct(index)">
                删除
              </el-button>
            </div>
          </div>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="isShow=false">取 消</el-button>
    <el-button type="primary" @click="btnOk">确 定</el-button>
  </span>
    </el-dialog>
    <!--    租赁订单选择-->
    <el-dialog
      title="请选择租赁订单"
      :visible.sync="orderListIsShow"
      width="50%"

    >
      <el-form inline :model="OrderFormData" label-width="100px">
        <el-form-item label="客户姓名" prop="customerName">
          <el-input style="width: 240px" v-model="OrderFormData.customerName" placeholder="请选择客户名" >
            <template slot="append">
              <el-button type="primary" @click="openProject">
                选择客户
              </el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="项目名称" prop="projectName">
          <el-input style="width: 240px" v-model="OrderFormData.projectName" placeholder="请输入项目名称" >
            <template slot="append">
              <el-button type="primary" @click="openProject">
                选择项目
              </el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="订单状态" prop="orderStatus">
          <el-select v-model="OrderFormData.orderStatus" placeholder="请选择">
            <el-option
              v-for="item in OrderOptions"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <el-button style="margin-bottom: 15px;margin-left: 100px;" size="small" type="success" @click="SearchOrderNo">查询
      </el-button>
      <el-table
        :data="OrderList"
        tooltip-effect="dark"
        style="width: 100%"
      >
        <el-table-column
          label="订单编号"
          prop="orderNo"
          width="150">
        </el-table-column>
        <el-table-column
          prop="customerName"
          label="客户名称"
          width="120">
        </el-table-column>
        <el-table-column
        prop="projectName"
        label="项目名"
        width="120">
        </el-table-column>
        <el-table-column
          prop="salesmanName"
          label="业务员"
          show-overflow-tooltip>
        </el-table-column>
        <el-table-column
          prop="orderDate"
          label="订单创建时间"
          show-overflow-tooltip>
        </el-table-column>
        <el-table-column
          label="操作">
          <template slot-scope="scope">
            <el-button type="success" size="small" @click="checked(scope.row.orderNo)">选择</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
    <!--    子集查看产品信息-->
    <el-dialog :visible.sync="isProductList" width="600">
      <el-table :data="productInfoList" tooltip-effect="dark" style="width: 90%">
        <el-table-column label="产品名称"
                         prop="productName"
                         align="center"
                         width="120">
        </el-table-column>
        <el-table-column
          prop="productNo"
          align="center"
          label="产品编号"
          width="180">
        </el-table-column>
        <el-table-column
          prop="model"
          label="型号"
          align="center"
          width="120">
        </el-table-column>
        <el-table-column
          prop="returnQuantity"
          align="center"
          label="申请回库产品数量"
          width="130">
        </el-table-column>
        <el-table-column
          prop="remark"
          label="备注"
          width="180">
        </el-table-column>
      </el-table>
    </el-dialog>
    <import-warehouse ref="importWarehouse" @showWarehouse="showWarehouse"></import-warehouse>
    <project-list ref="project" @showProject="showProject"></project-list>
    <!--  弹框  --物流弹框  -->
    <el-dialog title="修改物流" :visible.sync="logisticsShow" width="34%">
      <el-form :inline="true" :model="logisticsList" ref="logisticsfrom" label-width="120px" :rules="rules" class="demo-form-inline">
        <el-form-item label="项目名称"  prop="projectName">
          <el-input v-model="logisticsList.projectName"></el-input>
        </el-form-item>
        <el-form-item label="目的地"  prop="projectAddress">
          <el-input v-model="logisticsList.projectAddress"></el-input>
        </el-form-item>
        <el-form-item label="运输方式" prop="shippingType">
          <el-select  v-model="logisticsList.shippingType"  placeholder="运输方式">
            <el-option
              v-for="item in LogisticsOptions"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="出入库类型" prop="orderProperty">
          <el-select  v-model="logisticsList.orderProperty"  placeholder="出入库类型">
            <el-option
              v-for="item in  typeOptions"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item  label="司机姓名" prop="driverName">
          <el-input v-model="logisticsList.driverName" placeholder="请输入司机姓名"></el-input>
        </el-form-item>
        <el-form-item label="司机联系方式" prop="driverTel">
          <el-input  v-model="logisticsList.driverTel" placeholder="请输入司机联系方式"></el-input>
        </el-form-item>
        <el-form-item label="车辆信息" prop="vehicleInformation">
          <el-input  v-model="logisticsList.vehicleInformation" placeholder="请输入车辆信息"></el-input>
        </el-form-item>

        <el-form-item label="物流费用" prop="logisticsExpenses">
          <el-input v-model="logisticsList.logisticsExpenses" placeholder="请输入物流费用"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="logisticsShow = false">取 消</el-button>
        <el-button type="primary" @click="logisticsClick">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>

import {
  getBackWarehouseList,
  getChildNo,
  getBackWarehouseInfoApi,
  addBackWarehouseApi,
  editBackWarehouseApi,
  delBackWarehouseApi
} from '@/api/module/backapplication'
import {listOrder,getOrder} from '@/api/module/order'
import {provinceNameList} from '@/api/module/region';
import importWarehouse from "@/views/module/product/warehouse";
import {getDicts} from "@/api/system/dict/data";
import {editLogisticsApi,getLogisticsListApi,addLogisticsApi} from "@/api/module/logistics";
import ProjectList from '@/views/module/order/project'
export default {
  name: "BackApplication",
  components: {importWarehouse,ProjectList},
  data() {
    var validatePhone = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入电话号码'))
      }
      if (value.length !== 11) {
        callback(new Error('请输入正确电话号码'))
      }
      if (value.length !== 11) {
        callback(new Error('请输入正确电话号码'))
      }
      // 验证电话号码手机号码，包含至今所有号段? ?
      var ab = /^[1][3|4|5|6|7|8|9][0-9]{9}$/
      if (ab.test(value) === false) {
        callback(new Error('请输入正确电话号码'))
      }
      callback()
    }
    return {

      logisticsShow:false,//物流弹框
      logisticsList:{
        id:'',
        logisticsExpenses:'',
        vehicleInformation:'',
        driverTel:'',
        driverName:'',
        shippingType:'',

      },//物流列表
      //物流表验证
      rules:{
        shippingType:[{required:true,message:'请输选择运输方式',trigger:'blur'}],
        driverName:[{required:true,message:'请选输入司机姓名',trigger:'blur'}],
        driverTel:[{required:true,message:'请输入司机联系电话',trigger:'blur'},{ validator: validatePhone, trigger: 'blur' }],
        vehicleInformation:[{required:true,message:'请输入车辆信息',trigger:'blur'}],
        logisticsExpenses:[{required:true,message:'请输入物流费用',trigger:'blur'}],

      },
      LogisticsOptions:[],

      formData: {
        pageSize:10,
        pageNum:1,
        parentId: 0,//	是	Long	父级申请单id,第一次申请不传此属性或者传0
        returnApplyNo: null,//	是	String	回货单编号
        returnType:'1',//	是	String	回货/退货类型
        sponsor: null,//	是	Long	发起人
        sponsorName: null,//	是	string	发起人名称
        customerId: null,//	是	Long	客户
        customerName: null,//	是	string	客户名称
        orderId: null,//	是	Long	租赁销售订单
        orderNo: null,//	是	string	订单编号
        loadingAddressProvince: null,//	是	String	装货地址省份
        loadingAddressCity: null,//	是	String	装货地址市
        loadingAddressAre: null,//a	是	String	装货地址区
        loadingAddress: null,//	是	String	装货地址详细信息
        contactName: null,//	是	String	对接人名称
        contactTelephone: null,//	是	String	联系电话
        loadingDate: null,//	是	date	装货日期
        whetherLogisticsRequired: null,//	是	Long	是否需要物流安排车辆
        collaborator: null,//	是	string	协同人
        collaboratorName: null,//	是	BigDecimal	协同人名称
        remark: null,//	是	string	备注
        totalDailyRent: null,//	是	string	日租金合计

        shippingType: null,//	是	string	运输方式
        driverName: null,//e	是	string	司机姓名
        driverTel: null,//el	是	string	司机联系方式
        vehicleInformation: null,//	是	string	车辆信息
        warehouseId: null,//是	string	仓库id
        warehouseName: null,//e	是
        productList: [],
        salesman:null,
        salesmanName:null,
      },
      total:null,
      productInfoList: [],
      formRules: {
        returnApplyNo: [{required: true, message: '请输入如回库申请单编号', trigger: 'blur'}],
        returnType: [{required: true, message: '请选择回货类型', trigger: 'blur'}],
        sponsorName: [{required: true, message: '请输入发起人名称', trigger: 'blur'}],
        customerName: [{required: true, message: '请输入客户名称', trigger: 'blur'}],
        orderNo: [{required: true, message: '请输入订单编号', trigger: 'blur'}],
        contactName: [{required: true, message: '请输对接人名称', trigger: 'blur'}],
        contactTelephone: [{required: true, message: '请输入联系电话', trigger: 'blur'},{ validator: validatePhone, trigger: 'blur' }],
        // collaboratorName: [{required: true, message: '请输入协同人名称', trigger: 'blur'}],
        shippingType: [{required: true, message: '请选择运输方式', trigger: 'blur'}],
        driverName: [{required: true, message: '请输入司机姓名', trigger: 'blur'}],
        driverTel: [{required: true, message: '请输入司机联系电话', trigger: 'blur'},{ validator: validatePhone, trigger: 'blur' }],
        vehicleInformation: [{required: true, message: '请输入车辆信息', trigger: 'blur'}],
        warehouseName: [{required: true, message: '请选择仓库名', trigger: 'blur'}],
      },
      list: [],
      isShow: false,//是否弹框
      orderListIsShow: false,//租赁订单编号弹框
      OrderList: [],
      OrderFormData: {
        projectName:'',
        customerName: '',//客户名称
        orderStatus: '2'
      },
      regionOptions: null,
      loadingAddressOptions:[],
      //订单状态
      OrderOptions: [],
      //订单类型
      Ordertypes: [],
      //  是否需要物流
      options: [],//是否需要车辆
      optionProps: {
        value: 'label',
        label: 'label',
        children: 'children'
      },
      isProductList: false,
      typeOptions:[],
      productQuantityList:[],//产品数量
    }
  },
  created() {
    this.initData()
    this.getOrderOptions()//租赁订单状态
    this.getReturnTypeOptions()//回货类型
    this.getCarRequiredOptions()
  },
  methods: {
    //  回货入库单列表
    async initData() {
      let res = await getBackWarehouseList(this.formData)
      console.log(res)
      this.list = res.rows
      this.total=res.total
    },
    handleSizeChange(val) {
      this.formData.pageSize=val
      this.initData()
    },
    handleCurrentChange(val) {
      this.formData.pageNum=val
      this.initData()
    },
    //获取租赁订单状态
    async getOrderOptions() {
      let res = await getDicts('order_type')
      this.OrderOptions = res.data
    },
    //回货类型
    async getReturnTypeOptions() {
      let res = await getDicts('return_type')
      this.Ordertypes = res.data
    },
    async getCarRequiredOptions() {
      let res = await getDicts('car_required')
      this.options = res.data
    },

    //  查询功能
    search() {
      this.initData()
    },
    //重置
    resetList() {
      this.formData = {}
      this.initData()
    },
    //打开仓库
    openImportWarehouse() {
      this.$refs.importWarehouse.show(1);
    },
    showWarehouse(data) {
      //仓库
      console.log(data)
      this.formData.warehouseId = data.id
      this.formData.warehouseName = data.name

    },
//   租赁订单弹框打开
    openOrderListFn() {
      this.orderListIsShow = true
      this.SearchOrderNo()

    },
    //打开项目弹框
    openProject(){
      this.$refs.project.isShow=true
    },
    showProject(data){
      console.log(data)
      this.OrderFormData.projectName=data.projectName
      this.OrderFormData.customerName=data.customerName
    },
//新增弹框打开
    async openFn(val) {
        if (val === 0) {
          this.isShow = true
          this.getAreaList()
          this.formData.returnApplyNo = this.setOrderNo()
        } else {
          // 子集添加
          //根据订单状态判断是否完成
          let orderInfo=await listOrder({orderNo:val.orderNo})
          console.log(orderInfo.rows[0].orderStatus)
          this.productQuantityList=orderInfo.rows[0].productList
          if(orderInfo.rows[0].orderStatus==='3'||orderInfo.rows[0].orderStatus==='4'||orderInfo.rows[0].orderStatus==='5'||orderInfo.rows[0].orderStatus==='6' ){
            this.$message.warning('该申请单已完成')
          }else {
            this.isShow = true
            this.getAreaList()
            console.log(val)
            this.formData.parentId = val.id
            let res = await getChildNo({parentId: this.formData.parentId})
            console.log(res)

            this.formData.returnApplyNo = res.data.returnApplyNo

            this.formData.orderNo = val.orderNo
            this.formData.orderId = val.orderId
            let res1 = await getBackWarehouseInfoApi(val.id)
            console.log(res1)
            this.formData.sponsorName = res1.data.sponsorName
            this.formData.sponsor = res1.data.sponsor
            this.formData.customerId = res1.data.customerId
            this.formData.customerName = res1.data.customerName
            this.formData.totalDailyRent = res1.data.totalDailyRent
            this.formData.collaboratorName = res1.data.collaboratorName
            this.formData.warehouseId = res1.data.warehouseId//仓库ID
            this.formData.warehouseName = res1.data.warehouseName//仓库名
            this.formData.collaborator = res1.data.collaborator
            this.formData.collaboratorName = res1.data.collaboratorName
            this.formData.contactTelephone=res1.data.contactTelephone
            this.formData.loadingAddress=res1.data.loadingAddress
            this.formData.loadingDate=res1.data.loadingDate
            this.formData.whetherLogisticsRequired=res1.data.whetherLogisticsRequired
            this.formData.contactName=res1.data.contactName
            this.formData.salesmanName=res1.data.sponsorName
            this.formData.salesman=res1.data.salesman
            res1.data.productList.forEach(item=>{
              this.formData.productList.push({productName:item.productName,model:item.model})
            })
          }




      }



    },
    productInfo(val) {
      console.log(val)
      this.isProductList = true
      this.productInfoList = val.productList

    },
    //校验数量
   async isQuantityFn(val,index){
      console.log(val,index)
      console.log(this.productQuantityList)
      this.productQuantityList.forEach(item=>{
        if(item.model===val.model){
                 //查找已申请该型号所有数量

                 //和待回库数量比较
                 if(Number(item.quantity)-Number(item.returnApplyQuantity)<Number(val.returnQuantity)){
                           this.$message.error(`该型号待申请数量为:${Number(item.quantity)-Number(item.returnApplyQuantity)}`)
                              this.formData.productList[index].returnQuantity=null
                        }

        }
      })
    },
    //生成出库订单编号
    setOrderNo() {
      var date = new Date();
      var year = date.getFullYear();        //年 ,从 Date 对象以四位数字返回年份
      var month = date.getMonth() + 1;      //月 ,从 Date 对象返回月份 (0 ~ 11) ,date.getMonth()比实际月份少 1 个月
      var day = date.getDate();             //日 ,从 Date 对象返回一个月中的某一天 (1 ~ 31)
      //修改月份格式
      if (month >= 1 && month <= 9) {
        month = "0" + month;
      }
      //修改日期格式
      if (day >= 0 && day <= 9) {
        day = "0" + day;
      }

      let round = Math.floor(Math.random() * (999 - 100)) + 100;
      var currentFormatDate = `${year}${month}${day}`;
      return "HKSQ" + currentFormatDate + round;
    },

    //查询租赁订单
    async SearchOrderNo() {
      let res = await listOrder(this.OrderFormData)
      console.log(res)
      this.OrderList = res.rows

    },
    //  选中事件
    async checked(val) {
      this.formData.orderNo = val
      this.orderListIsShow = false
      let res = await listOrder(this.formData)
      console.log(res.rows[0])
      // this.orderFrom.id=res.rows[0].id//获取租赁订单id
      //
      this.formData.sponsor = res.rows[0].sponsor
      this.formData.sponsorName = res.rows[0].sponsorName
      this.formData.customerId = res.rows[0].customerId
      this.formData.customerName = res.rows[0].customerName
      this.formData.orderId = res.rows[0].id

      this.formData.totalDailyRent = res.rows[0].totalDailyRent
      this.formData.collaboratorName = res.rows[0].collaboratorName
      this.formData.warehouseId = res.rows[0].deliveryWarehouseId//仓库ID
      this.formData.warehouseName = res.rows[0].deliveryWarehouseName//仓库名
      this.formData.collaborator = res.rows[0].collaborator
      // this.formData.collaboratorName = res.rows[0].collaboratorName

      this.formData.productList =res.rows[0].productList
      this.formData.salesman=res.rows[0].salesman
      this.formData.salesmanName=res.rows[0].salesmanName


    },
   //获取地区选择
    getAreaList() {
      provinceNameList().then(res => {
        this.regionOptions = res.rows;
        let arr = JSON.parse(JSON.stringify(res.rows)) //深拷贝
        /*    arr.forEach(item => {
              item.children.forEach(cItem => delete cItem.children)
            })
            debugger*/
        this. loadingAddressOptions = arr
      })
    },
    changeFn(value){
    if(value && value.length>0){
      this.formData.loadingAddress=value[0]+value[1]+value[2]
      // console.log(value)
     this.formData.loadingAddressProvince=value[0]
      this.formData.loadingAddressCity=value[1]
      this.formData.loadingAddressAre=value[2]

    }

    },
    async editFn(val){
       this.isShow=true
      let res=await getBackWarehouseInfoApi(val.id)
      console.log(res)
      this.formData=res.data
    },
    //
    async btnOk() {

      try{
        await this.$refs.form.validate();
        if (this.formData.id) {
          await editBackWarehouseApi(this.formData)
        } else {

        if(this.formData.parentId){
          //子级新增 +校验
          let isChild=this.formData.productList.every(item=>{
            console.log(item.returnQuantity)
            return item.returnQuantity===null
          })
          if(isChild){
            // console.log(isChild)
          return  this.$message.warning('产品数量不能为零')
          }else{
            console.log(isChild)
            await addBackWarehouseApi(this.formData)
          }
        }else{
          await addBackWarehouseApi(this.formData)
        }

        }
        //  表单关闭
        this.isShow = false
        this.$message.success('操作成功')
      }catch(error){
        console.log(error)
      }



    },
    // 弹框关闭事件
    async closeDialog() {
      this.formData = {
        parentId: 0,//	是	Long	父级申请单id,第一次申请不传此属性或者传0
        returnApplyNo: null,//	是	String	回货单编号
        returnType: '1',//	是	String	回货/退货类型
        sponsor: null,//	是	Long	发起人
        sponsorName: null,//	是	string	发起人名称
        customerId: null,//	是	Long	客户
        customerName: null,//	是	string	客户名称
        orderId: null,//	是	Long	租赁销售订单
        orderNo: null,//	是	string	订单编号
        loadingAddressProvince: null,//	是	String	装货地址省份
        loadingAddressCity: null,//	是	String	装货地址市
        loadingAddressAre: null,//a	是	String	装货地址区
        loadingAddress: null,//	是	String	装货地址详细信息
        contactName: null,//	是	String	对接人名称
        contactTelephone: null,//	是	String	联系电话
        loadingDate: null,//	是	date	装货日期
        whetherLogisticsRequired: null,//	是	Long	是否需要物流安排车辆
        collaborator: null,//	是	string	协同人
        collaboratorName: null,//	是	BigDecimal	协同人名称
        remark: null,//	是	string	备注
        totalDailyRent: null,//	是	string	日租金合计

        shippingType: null,//	是	string	运输方式
        driverName: null,//e	是	string	司机姓名
        driverTel: null,//el	是	string	司机联系方式
        vehicleInformation: null,//	是	string	车辆信息
        warehouseId: null,//是	string	仓库id
        warehouseName: null,//e	是
        productList: []
      }
      await this.initData()
    },
    // 删除
    async delFn(val) {
      await this.$confirm('是否删除')
      await delBackWarehouseApi(val)
      this.$message.success('删除成功')
      await this.initData()
    },

    //  修改物流
    async logisticsBtn(val){
      console.log(val)
      this.logisticsShow=true
     await this.gettypeOptions()
     //  根据出入库申请单编号到物流表中查询数据
      let res=await getLogisticsListApi({returnApplyNo:val.returnApplyNo})
      // let res1=await listOrder({orderNo:val.orderNo})
     await this.getLogisticsList()
      if(res.rows.length!==0){
        this.logisticsList=res.rows[0]
      }else{
        this.logisticsList={
          orderId:val.orderId,
          orderNo:val.orderNo,
          warehouseName:val.warehouseName,
          returnApplyNo:val.returnApplyNo,
          warehousingNo:val.warehouseId,
          orderProperty:"2",//默认出库
          projectAddress: val.loadingAddress,
          projectName:val.projectName
        }
      }


    },
    //出入库类型
    async gettypeOptions(){
      let res=await getDicts('out_return_type')
      this.typeOptions=res.data
    },
    //获取物流运输方式
    async getLogisticsList(){
      let res=await getDicts('biz_shipping_type')
      this.LogisticsOptions=res.data
    },
    //  物流修改提交
    async logisticsClick(){
      try{
        await this.$refs.logisticsfrom.validate();
        if(this.logisticsList.id){
          await editLogisticsApi(this.logisticsList)
        }else{
          await addLogisticsApi(this.logisticsList)
        }
        this.logisticsShow=false
        await this.initData()
      }catch (error){
        console.log(error)
      }



    },

  }

}
</script>
